<?php

namespace Admin\Controller;

use Core\Constant;
use Core\Controller\AdminBaseController;
use Core\Exception\BusinessException;
use Core\Widget\WidgetFactory;
use Exception;
use Zend\Json\Json;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;

class BlockController extends AdminBaseController {


    public function blockClassAction(){

    }

    public function blockClassListAction(){
        try{

            $blockClassService = $this->getTableFinder()->getBlockClassTable();
            $allBlocClass = $blockClassService->fetchAll();
            $result = new JsonModel(array('success'=>true, 'rows'=>$allBlocClass->toArray()));
            return $result;
        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function getBlockUsageAction(){
        try{

            $id = $this->params()->fromPost('id');
            if(!$id){
                throw new BusinessException("Chưa truyền id để xóa ");
            }
            $blockClassService = $this->getTableFinder()->getBlockClassTable();
            $blockClass_Item = $blockClassService->getBlockClass($id);
            $allBlockInstance = $blockClass_Item->getAllBlockInstance();

            return new JsonModel(array('success'=>true,'data'=>
            array('name'=>$blockClass_Item->name, 'usage'=>$allBlockInstance->count())
            ));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function addBlockClassAction(){
        try{

            $name = $this->params()->fromPost('name');
            $description = $this->params()->fromPost('description');
            $program_code = $this->params()->fromPost('program_code');

            $data = array("name"=>$name,"description"=>$description,
                'program_code'=>$program_code);

            $blockClassService = $this->getTableFinder()->getBlockClassTable();
            $blockClassService->insert($data);

            return new JsonModel(array('success'=>true));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function removeBlockClassAction(){
        try{
            $id = $this->params()->fromPost('id');
            $blockClassService = $this->getTableFinder()->getBlockClassTable();
            $blockClass = $blockClassService->getBlockClass($id);
            if(!$blockClass)
                throw new BusinessException('Chưa truyền id để xóa');
            $resultSet = $blockClass->getAllBlockInstance();
            if($resultSet->count() > 0)
                throw new BusinessException('Đang tồn tại block thuộc nhóm này. Xóa các block trước khi xóa nhóm block');
                $blockClassService->delete($id);
             return new JsonModel(array('success'=>true));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function updateBlockClassAction(){
        try{
            $id = $this->params()->fromPost('id');
            $name = $this->params()->fromPost('name');
            $description = $this->params()->fromPost('description');
            $program_code = $this->params()->fromPost('program_code');

            $data = array("name"=>$name,"description"=>$description,
                'program_code'=>$program_code);

            $blockClassService = $this->getTableFinder()->getBlockClassTable();
            $blockClassService->update($data, $id);
            return new JsonModel(array('success'=>true));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }





    public function blockInstanceAction(){
        try{

            $blockClassTable = $this->getTableFinder()->getBlockClassTable();
            $allBlockClass = $blockClassTable->getAll();
            $tmp = 0;
            $resultArr = array();
            foreach ($allBlockClass as $blockClassItem) {
                if($tmp == 0)
                    $isExpanded = true;
                else
                    $isExpanded = false;

                $allBlockInstances = $blockClassItem->getAllBlockInstance();
                $blockInstanceArr = array();
                foreach ($allBlockInstances as $blockInstanceItem) {
                    array_push($blockInstanceArr, array('id'=>$blockInstanceItem->id,
                        'text'=>$blockInstanceItem->name
                    ));
                }
                array_push($resultArr, array('id'=>$blockClassItem->id,
                    'text'=>$blockClassItem->name, 'expanded'=>$isExpanded,
                    'items'=>$blockInstanceArr));
                $tmp++;
            }
            return new ViewModel(array('allBlockClass'=>$allBlockClass, 'treeData'=>Json::encode($resultArr)));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function getBlockEditorAction(){
        try{
            $blockInstanceId = $this->params()->fromPost('blockInstanceId');
            $blockInstanceTable =$this->getTableFinder()->getBlockInstanceTable();
            $blockInstance_Item = $blockInstanceTable->getBlockInstance($blockInstanceId);
            $blockClass_Item = $blockInstance_Item->getBlockClass();

            $widget_Factory = new WidgetFactory();
            $widget_Abstract = $widget_Factory->getWidget($blockClass_Item->program_code);

            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            $viewModel->setVariable('form',
                $widget_Abstract->form_update($blockInstance_Item, $blockClass_Item));
            $viewModel->setVariable('blockInstanceItem',
                $blockInstance_Item);
            return $viewModel;



        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function getBlockContentEditorAction(){
        try{
            $blockInstanceId = $this->params()->fromPost('blockInstanceId');
            $blockInstanceTable =$this->getTableFinder()->getBlockInstanceTable();
            $blockInstance_Item = $blockInstanceTable->getBlockInstance($blockInstanceId);
            $blockClass_Item = $blockInstance_Item->getBlockClass();

            $widget_Factory = new WidgetFactory();
            $widget_Abstract = $widget_Factory->getWidget($blockClass_Item->program_code);

            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            $viewModel->setVariable('form',
                $widget_Abstract->form_content($blockInstance_Item));
            $viewModel->setVariable('blockInstanceItem',
                $blockInstance_Item);
            return $viewModel;



        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function getBlockAddEditorAction(){
        try{
            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            $blockClassId = $this->params()->fromPost('blockClassId');
            $blockClassTable = $this->getTableFinder()->getBlockClassTable();
            $blockClass_Item = $blockClassTable->getBlockClass($blockClassId);
            if($blockClass_Item){
                $widgetFactory = new WidgetFactory();
                $widget_Abstract = $widgetFactory->getWidget($blockClass_Item->program_code);
                $viewModel->setVariable('form',
                    $widget_Abstract->form());
            }
            return $viewModel;

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    function deleteWidgetAction(){
        try{

            $widgetId = $this->params()->fromPost('id');
            $blockInstanceTable = $this->getTableFinder()->getBlockInstanceTable();
            $blockInstance = $blockInstanceTable->getBlockInstance($widgetId);
            if(!$blockInstance) throw new BusinessException("Block không tồn tại");

            $blockClass = $blockInstance->getBlockClass();
            $blockInstanceTable->delete($widgetId);

            return new JsonModel(array('success'=>true, 'classId'=>$blockClass->id));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }

    }

    function addWidgetAction(){
        try{

            $widgetName = $this->params()->fromPost('widgetName');
            $allParams = $this->params()->fromPost();
            $widgetFactory = new WidgetFactory();
            $widget_Abstract = $widgetFactory->getWidget($widgetName);
            $widgetId = $widget_Abstract->save($allParams);
            return new JsonModel(array('success'=>true, 'id'=>$widgetId));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }

    }

    function saveBlockContentAction(){
        try{

            $widgetName = $this->params()->fromPost('widgetName');

            $allParams = $this->params()->fromPost();
            $widgetFactory = new WidgetFactory();
            $widget_Abstract = $widgetFactory->getWidget($widgetName);
            $widgetId = $widget_Abstract->saveContent($allParams);
            return new JsonModel(array('success'=>true, 'id'=>$widgetId));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }

    }


    public function previewWidgetAction(){
        try{
            $widgetName = $this->params()->fromPost('widgetName');
            $allParams = $this->params()->fromPost();
            $widgetFactory = new WidgetFactory();
            $widget_Abstract = $widgetFactory->getWidget($widgetName);
            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            $viewModel->setVariable('form', $widget_Abstract->preview($allParams));
            return $viewModel;

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }


    function reloadTreeDataAction(){
        try{
            $resultArr = array();
            $blockClassTable = $this->getTableFinder()->getBlockClassTable();
            $allBlockClass = $blockClassTable->getAll();
            foreach ($allBlockClass as $blockClassItem) {
                $allBlockInstances = $blockClassItem->getAllBlockInstance();
                $blockInstanceArr = array();
                foreach ($allBlockInstances as $blockInstanceItem) {
                    array_push($blockInstanceArr, array('id'=>$blockInstanceItem->id,
                        'text'=>$blockInstanceItem->name
                    ));
                }
                array_push($resultArr, array('id'=>$blockClassItem->id,
                    'text'=>$blockClassItem->name,
                    'items'=>$blockInstanceArr));
            }

            return new JsonModel(array('success'=>true, 'data'=>$resultArr));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }

    }

    public function getBlockViewAction(){
        try{
            $blockInstanceId = $this->params()->fromPost('blockInstanceId');
            $blockInstanceTable = $this->getTableFinder()->getBlockInstanceTable();
            $blockInstanceItem = $blockInstanceTable->getBlockInstance($blockInstanceId);

            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            if($blockInstanceItem){
                $blockClass_Item = $blockInstanceItem->getBlockClass();
                $widgetFactory = new WidgetFactory();
                $widget_Abstract = $widgetFactory->getWidget($blockClass_Item->program_code);
                $viewModel->setVariable('render', $widget_Abstract->render($blockInstanceItem));

            } else
                $viewModel->setVariable('render', '');

            return $viewModel;

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }

    public function loadBlockContentAction(){
        try{
            $blockInstanceId = $this->params()->fromPost('blockInstanceId');
            $divId = $this->params()->fromPost('divId');

            $blockInstanceTable = $this->getTableFinder()->getBlockInstanceTable();
            $blockInstanceItem = $blockInstanceTable->getBlockInstance($blockInstanceId);

            $viewModel = new ViewModel();
            $viewModel->setTerminal(true);
            if($blockInstanceItem){
                $viewModel->setVariable('blockInstanceItem', $blockInstanceItem);
                $viewModel->setVariable('divId', $divId);
            } else
                $viewModel->setVariable('render', '');

            return $viewModel;

        }catch(Exception $e){
            $this->fireExceptionEvent($e, Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }



}